use crate::domain::dto::{
    EmptyDTO, IdDTO, SysRoleResAddDTO, SysRoleResPageDTO, SysRoleResUpdateDTO,
};
use crate::domain::vo::RespVO;
use crate::service::CONTEXT;
use axum::response::IntoResponse;
use axum::Json;

#[utoipa::path(
    post,
    path = "/admin/sys_role_add",
    request_body = SysRoleResAddDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn add(arg: Json<SysRoleResAddDTO>) -> impl IntoResponse {
    let vo = CONTEXT.sys_role_permission_service.add(&arg.0).await;
    return RespVO::from_result(vo).json();
}
#[utoipa::path(
    post,
    path = "/admin/sys_role_page",
    request_body = SysRoleResPageDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn page(arg: Json<SysRoleResPageDTO>) -> impl IntoResponse {
    let vo = CONTEXT.sys_role_permission_service.page(&arg.0).await;
    return RespVO::from_result(vo).json();
}

#[utoipa::path(
    post,
    path = "/admin/sys_role_layer_top",
    request_body = EmptyDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn layer_top(_arg: Json<EmptyDTO>) -> impl IntoResponse {
    let vo = CONTEXT.sys_role_service.finds_layer().await;
    return RespVO::from_result(vo).json();
}

#[utoipa::path(
    post,
    path = "/admin/sys_role_update",
    request_body = SysRoleResUpdateDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn update(arg: Json<SysRoleResUpdateDTO>) -> impl IntoResponse {
    let vo = CONTEXT.sys_role_permission_service.edit(&arg.0).await;
    return RespVO::from_result(vo).json();
}

#[utoipa::path(
    post,
    path = "/admin/sys_role_remove",
    request_body = IdDTO,
    responses(
        (status = 200, description = "success", body = IntoResponse),
        (status = 400, description = "fail", body = IntoResponse)
    )
)]
pub async fn remove(arg: Json<IdDTO>) -> impl IntoResponse {
    let role_id = arg.0.id.unwrap_or_default();
    let vo = CONTEXT
        .sys_role_permission_service
        .remove_role(&role_id)
        .await;
    return RespVO::from_result(vo).json();
}
